package com.shiikong_memory.yez.VirginGenerator;

import lombok.Data;
import org.springframework.core.io.ClassPathResource;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;

/**
 * 类创建时间：2024/4/29
 * 最后更新时间：2024/4/29
 * 圣女生成模块，负责生成圣女属性与介绍
 */
public class SaintGeneratedModule {
    private static Calendar Calender = Calendar.getInstance();
    private static List<String> Win;
    private static List<String> Defeat;

    static {
        try {
            String str;
            BufferedReader winBr=new BufferedReader(new InputStreamReader(new ClassPathResource("Data/VirginWinData.txt").getInputStream()));
            Win = new ArrayList<>();
            // 通过readLine()方法按行读取字符串
            while ((str = winBr.readLine()) != null) {
                Win.add(str);
            }
            BufferedReader defeatBr=new BufferedReader(new InputStreamReader(new ClassPathResource("Data/VirginDefeatData.txt").getInputStream()));
            Defeat = new ArrayList<>();
            // 通过readLine()方法按行读取字符串
            while ((str = defeatBr.readLine()) != null) {
                Defeat.add(str);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static SaintData create(String name){
        Date date = new Date();
        long seed =0;
        SaintData saintData = new SaintData();
        for(char iChar: name.toCharArray()){
            seed += iChar;
        }
        Random random = new Random(seed);
        saintData.setPhotoaffinity(random.nextInt(10)+1);
        saintData.setAbdominal(random.nextInt(11));
        saintData.setSensitiveness(random.nextInt(11));
        saintData.setCharm(random.nextInt(10)+1);
        saintData.setChestSize((char) (random.nextInt(6)+65));
        saintData.setCaput(random.nextInt(16));
        creatRecordData(saintData, random.nextInt(),date);
        return saintData;
    }

    public static String getCardJson(SaintData saintData){
        String caput = "null";
        switch (saintData.getCaput()){
            case 0 -> caput = "INTP（思绪飞扬的学者）";
            case 1 -> caput = "INTJ（独立自主的专家）";
            case 2 -> caput = "ENTP（大雄的机器猫）";
            case 3 -> caput = "ENTJ（天生的领导者）";
            case 4 -> caput = "ENFJ（谆谆教诲的教育家）";
            case 5 -> caput = "INFJ（精神世界的引路人）";
            case 6 -> caput = "INFP（完美主义的知心人）";
            case 7 -> caput = "ENFP（追梦人）";
            case 8 -> caput = "ESFP（不可或缺的大活宝）";
            case 9 -> caput = "ISFP（静美的艺术家）";
            case 10 -> caput = "ISTP（谦逊的手艺人）";
            case 11 -> caput = "ISFPESTP（活在当下的践行者）";
            case 12 -> caput = "ISFJ（具奉献精神的保护者）";
            case 13 -> caput = "ISTJ（一丝不苟的检查者）";
            case 14 -> caput = "ESFJ（盛情难却的东道主）";
            case 15 -> caput = "ESTJ（天生的管理者）";

        }
        return String.format(CardJson,saintData.getPhotoaffinity(),
                saintData.getAbdominal(),
                saintData.getSensitiveness(),
                saintData.getCharm(),
                saintData.getChestSize(),
                caput,
                saintData.getWinString(),
                saintData.getDefeatString(),
                saintData.getTrophyString());
    }

    private static void creatRecordData(SaintData saintData,int r,Date date){
        Calender.setTime(date);
        r += Calender.get(Calendar.YEAR) + Calender.get(Calendar.MONTH) + Calender.get(Calendar.DATE);
        Random random = new Random(r);
        List<String> win = new ArrayList<>();
        List<String> defeat = new ArrayList<>();
        List<String> trophy = new ArrayList<>();
        int size = random.nextInt(3);
        List<Integer> Line = new ArrayList<>();
        for (int i=0;i<size;i++){
            int i1;
            do {
                i1 = random.nextInt(Win.size());
            }while (Line.contains(i1));
            Line.add(i1);
            String[] split = Win.get(i1).split("\\|");
            if (split.length == 1){
                win.add(String.format(split[0],random.nextInt(5)+1));
            }else {
                win.add(String.format(split[0],random.nextInt(5)+1));
                trophy.add(split[1]);
            }
        }
        Line.clear();
        size = random.nextInt(3);
        for (int i=0;i<size;i++){
            int i1;
            do {
                i1 = random.nextInt(Defeat.size());
            }while (Line.contains(i1));
            Line.add(i1);
            String[] split = Defeat.get(i1).split("\\|");
            if (split.length == 1){
                defeat.add(String.format(split[0],random.nextInt(5)+1));
            }else {
                defeat.add(String.format(split[0],random.nextInt(5)+1));
                trophy.add(split[1]);
            }
        }
        if (win.isEmpty()) win.add("无");
        if (defeat.isEmpty()) defeat.add("无");
        if (trophy.isEmpty()) trophy.add("无");
        saintData.setWinData(win);
        saintData.setDefeatData(defeat);
        saintData.setTrophyData(trophy);
    }

    @Data
    public static class SaintData{
        /**
         * 数值
         */
        //圣光亲和度(1-10)
        private int Photoaffinity;
        //腹黑指数(0-10)
        private int Abdominal;
        //敏感度(0-10)
        private int sensitiveness;
        //魅力(1-10)
        private int charm;

        /**
         * 特点
         */
        //胸部大小(1-6)
        private char chestSize;
        //人格(0-15)(参考：https://zhuanlan.zhihu.com/p/106634022)
        private int caput;

        /**
         * 数据
         */
        //战绩（胜）
        private List<String> winData;
        //战绩（败）
        private List<String> defeatData;
        //战利品
        private List<String> trophyData;

        public String getWinString() {
            StringBuffer s = new StringBuffer();
            for (int i=0;i<winData.size();i++, s.append("\\n")) s.append(winData.get(i));
            return s.toString();
        }

        public String getDefeatString() {
            StringBuffer s = new StringBuffer();
            for (int i=0;i<defeatData.size();i++, s.append("\\n")) s.append(defeatData.get(i));
            return s.toString();
        }

        public String getTrophyString() {
            StringBuffer s = new StringBuffer();
            for (int i=0;i<trophyData.size();i++, s.append("\\n")) s.append(trophyData.get(i));
            return s.toString();
        }
    }

    private static String CardJson = "[\n" +
            "  {\n" +
            "    \"type\": \"card\",\n" +
            "    \"theme\": \"secondary\",\n" +
            "    \"size\": \"lg\",\n" +
            "    \"modules\": [\n" +
            "      {\n" +
            "        \"type\": \"header\",\n" +
            "        \"text\": {\n" +
            "          \"type\": \"plain-text\",\n" +
            "          \"content\": \"预言结果\"\n" +
            "        }\n" +
            "      },\n" +
            "      {\n" +
            "        \"type\": \"divider\"\n" +
            "      },\n" +
            "      {\n" +
            "        \"type\": \"section\",\n" +
            "        \"text\": {\n" +
            "          \"type\": \"paragraph\",\n" +
            "          \"cols\": 3,\n" +
            "          \"fields\": [\n" +
            "            {\n" +
            "              \"type\": \"kmarkdown\",\n" +
            "              \"content\": \"**圣光亲和度**\\n%s\"\n" +
            "            },\n" +
            "            {\n" +
            "              \"type\": \"kmarkdown\",\n" +
            "              \"content\": \"**腹黑指数**\\n%s\"\n" +
            "            },\n" +
            "            {\n" +
            "              \"type\": \"kmarkdown\",\n" +
            "              \"content\": \"**敏感度**\\n%s\"\n" +
            "            }\n" +
            "          ]\n" +
            "        }\n" +
            "      },\n" +
            "      {\n" +
            "        \"type\": \"section\",\n" +
            "        \"text\": {\n" +
            "          \"type\": \"paragraph\",\n" +
            "          \"cols\": 3,\n" +
            "          \"fields\": [\n" +
            "            {\n" +
            "              \"type\": \"kmarkdown\",\n" +
            "              \"content\": \"**魅力**\\n%s\"\n" +
            "            },\n" +
            "            {\n" +
            "              \"type\": \"kmarkdown\",\n" +
            "              \"content\": \"**胸部大小**\\n%c\"\n" +
            "            },\n" +
            "            {\n" +
            "              \"type\": \"kmarkdown\",\n" +
            "              \"content\": \"**人格**\\n%s\"\n" +
            "            }\n" +
            "          ]\n" +
            "        }\n" +
            "      },\n" +
            "      {\n" +
            "        \"type\": \"section\",\n" +
            "        \"text\": {\n" +
            "          \"type\": \"kmarkdown\",\n" +
            "          \"content\": \"***今日战绩***\"\n" +
            "        }\n" +
            "      },\n" +
            "      {\n" +
            "        \"type\": \"section\",\n" +
            "        \"text\": {\n" +
            "          \"type\": \"paragraph\",\n" +
            "          \"cols\": 2,\n" +
            "          \"fields\": [\n" +
            "            {\n" +
            "              \"type\": \"kmarkdown\",\n" +
            "              \"content\": \"**胜**\\n%s\"\n" +
            "            },\n" +
            "            {\n" +
            "              \"type\": \"kmarkdown\",\n" +
            "              \"content\": \"**败**\\n%s\"\n" +
            "            }\n" +
            "          ]\n" +
            "        }\n" +
            "      },\n" +
            "      {\n" +
            "        \"type\": \"section\",\n" +
            "        \"text\": {\n" +
            "          \"type\": \"kmarkdown\",\n" +
            "          \"content\": \"  ***战利品***\"\n" +
            "        }\n" +
            "      },\n" +
            "      {\n" +
            "        \"type\": \"section\",\n" +
            "        \"text\": {\n" +
            "          \"type\": \"plain-text\",\n" +
            "          \"content\": \"%s\"\n" +
            "        }\n" +
            "      },\n" +
            "      {\n" +
            "        \"type\": \"divider\"\n" +
            "      },\n" +
            "      {\n" +
            "        \"type\": \"context\",\n" +
            "        \"elements\": [\n" +
            "          {\n" +
            "            \"type\": \"plain-text\",\n" +
            "            \"content\": \"生成结果均为虚构，请勿当真\"\n" +
            "          }\n" +
            "        ]\n" +
            "      }\n" +
            "    ]\n" +
            "  }\n" +
            "]";
}
